package org.cc.cloud;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.loadbalancer.IRule;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.amqp.rabbit.support.CorrelationData;
@Configuration
public class Config {
	
	@Bean
	 public IRule  getRule(){
		 return new MyRibbonRule();
	 }
	@Autowired(required=false)
	@MyLoadBalanced
   List<RestTemplate>  tpls=Collections.emptyList();
	



	
/*   @Bean
   public SmartInitializingSingleton  initializing(){
	 return  new SmartInitializingSingleton(){

		public void afterSingletonsInstantiated() {
			for(RestTemplate  tp:tpls){
				List<ClientHttpRequestInterceptor> list= tp.getInterceptors();
				list.add(new MyInterceptor());
				tp.setInterceptors(list);
			}
			//System.out.println(tpls.size());
			// TODO Auto-generated method stub
			
		}
		   
	   };
   }*/
	
	//   publisher-confirms: true 
	//rabbit.setMandatory(true);
	//设置了以上两种才能使用setReturnCallback
	@Bean
	public RabbitTemplate rabbitTemplate1(ConnectionFactory connectionFactory) {
		RabbitTemplate rabbit = new RabbitTemplate(connectionFactory);
		rabbit.setMandatory(true);
		//rabbit.setMandatoryExpression(new SpelExpressionParser().parseExpression("(1+2) >= 3");
		//		如果mandatory有设置，则当消息不能路由到队列中去的时候，会触发return method, 
		rabbit.setReturnCallback(new ReturnCallback() {
			public void returnedMessage(Message message, int replyCode, String replyText, String exchange,
					String routingKey) {
				System.out.println("===========returnedMessage===========");
				System.out.println("replyCode: " + replyCode);
				System.out.println("replyText: " + replyText);
				System.out.println("exchange: " + exchange);
				System.out.println("routingKey: " + routingKey);
				System.out.println(new String(message.getBody()));
			}
		});
	
//		//publisher-confirms要设置这个属性为true才能使用
		//需要发送的时候使用	rabbit.send(exchange, routingKey, message, correlationData);配合使用
		//correlationData 可以设置为每个实体的id方便查看
		rabbit.setConfirmCallback(new ConfirmCallback() {
			/**
			 * correlationData唯一标识
			 * 有了这个唯一标识，我们就知道可以确认（失败）哪一条消息了
			 */
			public void confirm(CorrelationData correlationData, boolean ack, String cause) {
				System.out.println("-------------收到消息确认啦-----------");
				if(ack) {
					System.out.println(correlationData.getId() + " 已经成功发送");
				} else {
					System.out.println(correlationData.getId() + " 发送失败，原因为：" + cause);
				}
			}
		});
		return rabbit;
}
}
